/*
 * Copyright (c) 2010, Nikolaus Moll
 * All rights reserved.
 */
package net.miblounge.gradecalculator.dataprovider;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

import net.miblounge.gradecalculator.core.AbstractDataProvider;
import net.miblounge.gradecalculator.core.descriptors.StudyRegulationsDescription;
import net.miblounge.gradecalculator.core.descriptors.University;

public class DataProvider extends AbstractDataProvider {
	private static final String DIRUNIVERSITY = "resources/universities";
	private static final String DIRSPODESCRIPTION = "resources/universities";
	private static final String EXTUNIVERSITY = ".unixml";
	private static final String EXTSPODESCRIPTION = ".srdx";
	
	public static void initialize()
	{
		new DataProvider();
	}
	
	@Override
	public University getUniversity(final String name) {
		return University.createFromXML(DIRUNIVERSITY + "/" + name + EXTUNIVERSITY);
	}

	@Override
	public StudyRegulationsDescription getSPODescription(final String name) {
		return StudyRegulationsDescription.createFromXML(DIRSPODESCRIPTION + "/" + name.replace('.', File.separatorChar) + EXTSPODESCRIPTION);
	}

	protected List<String> getDirectories(final String root) {
		final List<String> result = new ArrayList<String>();
		final File importerDirectory = new File(root);

        final File[] fileList = importerDirectory.listFiles();
        for (File file : fileList) {
        	if (!file.isDirectory()) {
        		continue;
        	}
        	if (file.getName().startsWith(".")) {
        		continue;
        	}
        	result.add(file.getName());
        }
        return result;
	}

	protected List<String> getFiles(final String root, final String extension) {
		final List<String> result = new ArrayList<String>();
		final File importerDirectory = new File(root);

        final File[] fileList = importerDirectory.listFiles();
        for (File file : fileList) {
        	if (file.isDirectory()) {
        		continue;
        	}
        	if (!file.getName().endsWith(extension)) {
        		continue;
        	}
        	
        	final String name = file.getName().substring(0, file.getName().length() - extension.length());
        	result.add(name);
        }
        return result;
	}

	@Override
	public List<String> getUniversities() {
		final List<String> result = new ArrayList<String>();
		final File importerDirectory = new File(DIRUNIVERSITY);
		final FilenameFilter filter = new FilenameFilter() {
            public boolean accept(final File dir, final String name) {
                return name.endsWith(EXTUNIVERSITY);
            }
        };

        final File[] fileList = importerDirectory.listFiles(filter);
        for (File file : fileList) {
        	if (file.isDirectory()) {
        		continue;
        	}
        	final String filename = file.getName();
        	final String universityName = filename.substring(0, filename.lastIndexOf('.'));
        	result.add(universityName);
        }
        return result;
	}

//	protected <T> T getFromList(final List<T> list, final T o)
//	{
//		for (T t : list) {
//			if (o.equals(t)) {
//				return t;
//			}
//		}
//		
//		return null;
//	}
	
//	@Override
//	public List<CourseOfStudiesInformation> getCourseOfStudiesByUniversity(final String university) {
//		System.out.println("getCourseOfStudiesByUniversity(" + university + ")");
//		
//		final List<CourseOfStudiesInformation> result = new ArrayList<CourseOfStudiesInformation>();
//		final File importerDirectory = new File(DIRSPODESCRIPTION);
//		final FilenameFilter filter = new FilenameFilter() {
//            public boolean accept(final File dir, final String name) {
//                return name.startsWith(university) && name.endsWith(EXTSPODESCRIPTION);
//            }
//        };
//
//        final File[] fileList = importerDirectory.listFiles(filter);
//        for (File file : fileList) {
//        	if (file.isDirectory()) {
//        		continue;
//        	}
//
//        	final String filename = file.getName();
//        	final String[] parts = filename.split("\\.");
//        	if (parts.length != 5) {
//        		continue;
//        	}
//        	
//        	final CourseOfStudiesInformation c = new CourseOfStudiesInformation(parts[0], parts[1], parts[2]);
//           	if (result.contains(c)) {
//           		final CourseOfStudiesInformation ex = result.get(result.indexOf(c));
//           		ex.addSemester(parts[3]);
//        	} else {
//           		c.addSemester(parts[3]);
//        		result.add(c);
//        	}
//        }
//        return result;
//	}

	@Override
	public List<String> getDegrees(String university) {
		System.out.println("getDegrees(" + university + ")");
		
		final String root = DIRUNIVERSITY + File.separator + university;
		return getDirectories(root);
	}

	@Override
	public List<String> getCourses(String university, String degree) {
		System.out.println("getCourses(" + university + ", " + degree + ")");
		
		final String root = DIRUNIVERSITY + File.separator + university + File.separator + degree;
		return getDirectories(root);
	}

	@Override
	public List<String> getSemesters(String university, String degree,
			String course) {
		System.out.println("getSemesters(" + university + ", " + degree + ", " + course + ")");
		
		final String root = DIRUNIVERSITY + File.separator + university + File.separator + degree + File.separator + course;
		return getFiles(root, EXTSPODESCRIPTION);
	}

}
